//
//  MSEmployeeAuditController.m
//  觅尚
//
//  Created by No.8 on 2019/4/28.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSEmployeeAuditController.h"
#import "MSEmployCell.h"
#import "MSHomeMeCell.h"
#import "MSEmployeeModel.h"

#define pageSize 20


@interface MSEmployeeAuditController ()<UITableViewDelegate,UITableViewDataSource,MSEmployCellDelegate>
//通用导航栏
{
    CustomNavigationBar *naviagtionBar;
    NSInteger page;
    NSInteger count;
}

/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *ExamineArray;


/** 员工id*/
@property(nonatomic,strong)NSString *UidStr;


@end

@implementation MSEmployeeAuditController

/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.ExamineArray = [NSMutableArray new];
    
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavigationBar];
    
    [self CreattableView];
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}

- (void)ReloadData{
    [self.ExamineArray removeAllObjects];
    
    [MBManager showLoading];
    
    NSDictionary * params = @{@"roleId":[WYCAccountTool unarchivebelongRolid],@"busId":[WYCAccountTool unarchivebelongBusid],@"pageCount":@(page),@"counSize":@(pageSize)};
    [HTTPRequest POST:MSgetToExaminePerson params:params succeed:^(id responseObject) {
        WYCLog(@"------------%@",params);
        if (CodeStr) {
            count = [responseObject[@"data"][@"count"] integerValue];
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            NSMutableArray *temporaryArray = [NSMutableArray new];
            temporaryArray = [MSEmployeeModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userList"]];
            self.ExamineArray = temporaryArray;
        }else{
            
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        WYCLog(@"------------%@",error);
        [self.tableView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)FootderRefreshReloadData{
    
    [self.ExamineArray removeAllObjects];
    
    [MBManager showLoading];
    
    NSDictionary * params = @{@"roleId":[WYCAccountTool unarchivebelongRolid],@"busId":[WYCAccountTool unarchivebelongBusid],@"pageCount":@(page),@"counSize":@(pageSize)};
    [HTTPRequest POST:MSgetToExaminePerson params:params succeed:^(id responseObject) {
        WYCLog(@"------------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            self.ExamineArray = [MSEmployeeModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userList"]];
        }else{
            
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page --;
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        WYCLog(@"------------%@",error);
        [self.tableView.mj_footer endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}

- (void)ButtonClickUid:(NSString *)Uid{
    self.UidStr = Uid;
}
/** 表视图*/
- (void)CreattableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    //    if (!_tableView) {
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    //    }
    //    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.ExamineArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSEmployCell * cell = [MSEmployCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.EmployeeModel = self.ExamineArray[indexPath.row];
    cell.operationButtonDelegate = self;
    [cell.AdoptButton addTarget:self action:@selector(AdoptButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [cell.NoAdoptButton addTarget:self action:@selector(NoAdoptButtonClick) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)AdoptButtonClick{
    
    [MBManager showLoading];
    NSDictionary *params = @{@"uid":self.UidStr,@"state":@"1"};
    WYCLog(@"==============%@",params);
    [HTTPRequest POST:MSUpdateToExamine params:params succeed:^(id responseObject) {
        WYCLog(@"==============%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self ReloadData];
        }else{
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}

- (void)NoAdoptButtonClick{
    [MBManager showLoading];
    NSDictionary *params = @{@"uid":self.UidStr,@"state":@"2"};
    WYCLog(@"==============%@",params);
    [HTTPRequest POST:MSUpdateToExamine params:params succeed:^(id responseObject) {
        WYCLog(@"==============%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self ReloadData];
        }else{
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
    
}
- (void)createNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"员工管理";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
